package com.asjy.controller;


import com.asjy.dao.UserDao;
import com.asjy.exception.MyException;
import com.asjy.po.Person;
import com.asjy.po.User;
import com.asjy.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * @Auther: lilinlin
 * @Date: 2021/4/5
 * @Description: com.asjy.controller
 * * @version: 1.0
 */
//后端控制器
@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private HttpSession session;
    @Resource
    private UserService userService;
    @Resource
    private UserDao userDao;

    //异常处理案例
    @RequestMapping("/exception1")
    public String exception1(){
        //模拟异常
        System.out.println("封装参数");
        //调用service
        int  i=10/0;//ArithmeticException
        System.out.println("执行中...");
        return "success";
    }

    //异常处理案例
    @RequestMapping("/exception2")
    public String exception2() throws MyException{
        try {
            //模拟异常
            System.out.println("封装参数");
            //调用service
            int  i=10/0;
            System.out.println("执行中...");
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            //异常类型转换
            throw new MyException("获取用户列表错误");
        }
    }







    @RequestMapping("/userList")
    //1.接受参数，封装参数 2.分发转向
    //执行流程：
    // @RequestParam("id")  id:前端表单项的name的名
    public String getUser(Person person, Model model) {
        User user = userService.getUser(person.getId());
        //request.setAttribute(k,v)
        model.addAttribute("user1", user);
        System.out.println("///////");
        //逻辑视图
        //请求转发
        return "userList";
    }

    @RequestMapping("/toRegister")
    public String toRegister() {
        System.out.println(".....+++++");
        return "register";
    }

    /**
     * 验证用户名是否重复
     *
     * @return 字符串 ok代表可用 no代表已存在
     */
    @RequestMapping("/checkUserName")
    //返回字符串到客户端，不走视图解析器了
    @ResponseBody
    public String checkUserName(String username) {
        Integer count = userService.checkUserName(username);
        System.out.println(count + "/////////");
        if (count > 0) {
            //response.getWriter.out(")
            return "no";
        } else {
            return "yes";
        }
    }

    /**
     * @param user 用户信息
     * @return 无返回值
     */
    @RequestMapping("/doRegister")
    public String doRegister(User user, Model model) {
        System.out.println(user.getUsername() + "....++++");
        //后端验证  作用
        if (user.getUsername() == null || user.getUsername().trim() == "") {
            model.addAttribute("userNameMsg", "用户名错误");
            model.addAttribute("username", user.getUsername());
            return "register";
        } else {
            userService.addUser(user);
            return "login";
        }


    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @RequestMapping("/doLogin")
    public String doLogin(User user, @RequestParam(value = "remember", required = false) String rem, HttpServletResponse response, Model model) {
        User u = userService.checkUser(user);
        //用户存在 首页
        if (u != null) {
            //1.保存用户信息到session
            session.setAttribute("user", u);
            //2.是否保存cookie
            Cookie cookieUsername = null;
            Cookie cookiePassword = null;
            if (rem != null) {
                cookieUsername = new Cookie("username", u.getUsername());
                cookiePassword = new Cookie("password", u.getPassword());
                cookieUsername.setPath("/");
                cookiePassword.setPath("/");
                cookieUsername.setMaxAge(60 * 60 * 24 * 7);
                cookiePassword.setMaxAge(60 * 60 * 24 * 7);

            } else {
                cookieUsername = new Cookie("username", null);
                cookiePassword = new Cookie("password", null);
                cookieUsername.setPath("/");
                cookiePassword.setPath("/");
                cookieUsername.setMaxAge(0);
                cookiePassword.setMaxAge(0);

            }
            response.addCookie(cookieUsername);
            response.addCookie(cookiePassword);
            return "index";
        } else {
            //用户不存在 回到 登录页
            model.addAttribute("msg", "用户名或密码错误");
            return "login";
        }


    }

    @RequestMapping("/toMyInfo")
    public String toMyInfo() {

            return "myInfo";


    }

    @RequestMapping("/doMyInfo")
    public String doMyInfo(User user, Model model) {
        User sysUser = (User) session.getAttribute("user");
        user.setuId(sysUser.getuId());
        //修改用户信息
        Integer count = userService.saveMyInfo(user);
        System.out.println(count + "........");
        if (count == 1) {
            return "login";
        } else {
            model.addAttribute("msg", "用户信息修改错误");
            return "myInfo";
        }

    }
}